package com.git.controller;

import com.git.bean.District;
import com.git.bean.House;
import com.git.bean.Htype;
import com.git.bean.SearchInfo;
import com.git.service.HouseService;
import com.git.service.HtypeService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.swing.*;
import java.util.List;

/**
 * @author 金振宇
 * @describe
 */
@Controller
@RequestMapping("/house")

//sdfgsdfg
public class HouseController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Reference(url = "dubbo://localhost:20880")
    private HouseService houseService;

    @Reference(url = "dubbo://localhost:20880")
    private HtypeService htypeService;

    @RequestMapping("findHouse")
    public String findHouse(Model model,@RequestParam(defaultValue = "1")Integer pageNum){
        logger.info("查询商品信息",pageNum);
        try {
            PageInfo<House> pageInfo = houseService.findAllHouse(pageNum,null);
            model.addAttribute("pageInfo",pageInfo);
            return "guanli";
        }catch (Exception e){
            logger.error("查询商品信息失败",e);
        }
        return "erro";
    }


    @RequestMapping("toUpdate")
    public String toUpdate(Integer id ,Model model){
        logger.info("去修改商品信息",id);
        try {
            House house = houseService.findHouseById(id);
            List<Htype> htypeList = htypeService.findAllHtype();
            List<District> districtList = houseService.findAllDistrict();
            System.out.println(house);
            model.addAttribute("house",house);
            model.addAttribute("districtList",districtList);
            model.addAttribute("htypeList",htypeList);
            return "xiugai";
        }catch (Exception e){
            logger.error("去修改商品信息失败",e);
        }
        return "erro";
    }

    @RequestMapping("updateHouse")
    public String updateHouse(House house){
        logger.info("修改商品信息",house);
        try {
            houseService.updateHouse(house);
            return "redirect:/house/findHouse";
        }catch (Exception e){
            logger.error("修改商品信息失败",e);
        }
        return "erro";
    }

    @RequestMapping("/toList")
    public String toList(Model model, @RequestParam(defaultValue = "1") Integer pageNum, SearchInfo searchInfo){
        try {
            logger.info("登陆后测试展示所有");
            PageInfo<House> pageInfo = houseService.findAllHouse(pageNum,searchInfo);
            System.out.println("从前台查出来的"+searchInfo);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("searchInfo",searchInfo);
            return "/list.html";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/list.html";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        try {
            logger.info("去添加房屋出租信息");
            List<Htype> htypeList = htypeService.findAllHtype();
            model.addAttribute("htypeList",htypeList);
            return "fabu";
        } catch (Exception e) {
            logger.error("去添加房屋出租信息失败",e);
        }
        return "erro";
    }

    @RequestMapping("/addHouse")
    public String addHouse(House house){
        try {
            logger.info("添加房屋出租信息",house);
            houseService.addHouse(house);
            return "redirect:/house/findHouse";
        } catch (Exception e) {
            logger.error("添加房屋出租信息失败",e);
        }
        return "erro";
    }

    @RequestMapping("/toDetail")
    public String toDetail(Integer hid, Model model){
        logger.info("去详细界面"+hid);
        House house = houseService.findHouseById(hid);
        model.addAttribute("house",house);
        return "/details.html";
    }
}
